﻿using carShop.Data;
using carShop.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using carShop.Models.Enums;

namespace carShop.Web.Controllers
{
    [RoutePrefix("api/Car")]
    public class CarController : ApiController
    {
        private UnitOfWork uow = new UnitOfWork();

        // GET api/<controller>
        [HttpGet]
        public IHttpActionResult Get()
        {
            try
            {
                var cars = uow.CarRepository.GetWithIncludeProperties("Image,Video").ToList();
                return Ok(cars);
            }
            catch (Exception)
            {

                return InternalServerError();
            }
        }

        // GET api/<controller>/5
        [HttpGet]
        [Route("{id:int}", Name = "GetCarById")]
        public IHttpActionResult Get(int id)
        {
            try
            {
                var cars =  uow.CarRepository.GetCarByIdWithIncludeProperties(id, "Image,Video").FirstOrDefault();
                if (cars == null)
                    return NotFound();

                return Ok(cars);
            }
            catch (Exception)
            {

                return InternalServerError();
            }
        }

        [Route("GetBrandNameList")]
        [HttpGet]
        public IHttpActionResult GetBrandNameList()
        {
            var brandName = (CarBrand[])Enum.GetValues(typeof(CarBrand));
            return Ok(brandName.ToList());
        }

        [Route("GetCarTypeList")]
        [HttpGet]
        public IHttpActionResult GetCarTypeList()
        {
            var brandName = (CarType[])Enum.GetValues(typeof(CarType));
            return Ok(brandName.ToList());

        }

        // POST api/<controller>
        [Route("Add")]
        [HttpPost]
        [Authorize]
        public IHttpActionResult Add([FromBody]Car Car)
        {
            try
            {
                uow.CarRepository.Insert(Car);
                uow.Commit();

                string uri = Url.Link("GetCarById", new { id = Car.Id });

                return Created(new Uri(uri), Car);
            }
            catch (Exception ex)
            {

                return InternalServerError(ex);
            }
        }

        [Route("UpdateCar")]
        [HttpPut]
        public IHttpActionResult UpdateCar([FromBody]Car Car)
        {
            try
            {
                Car.State = State.Modified;
                foreach (var i in Car.Image)
                {
                    i.State = State.Modified;
                }
                uow.CarRepository.Update(Car);
                uow.Commit();
                return Ok();
            }
            catch (Exception)
            {

                return InternalServerError();
            }
        }


        // DELETE api/<controller>/5
        [Route("{id:int}")]
        [HttpDelete]
        public IHttpActionResult Delete(int id)
        {
            try
            {
                uow.CarRepository.Delete(id);
                uow.Commit();
                return Ok();
            }
            catch (Exception)
            {
                return InternalServerError();
            }
        }
    }
}